#include "widget.h"
#include "ui_widget.h"
#include "HRVCompute/coder_array.h"
#include "HRVCompute/freqDomainHRV.h"
#include "HRVCompute/freqDomainHRV_terminate.h"
#include "HRVCompute/freqDomainHRV_types.h"
#include "HRVCompute/nonlinearHRV.h"
#include "HRVCompute/poincareHRV.h"
#include "HRVCompute/rt_nonfinite.h"
#include "HRVCompute/timeDomainHRV.h"
#include "HRVCompute/timeFreqHRV.h"
#include <string.h>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include "detrend/priors/detrendIBI_priors.h"




Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    coder::array<double, 2U> ibi;
    struct8_T output;
    QList<double> data=readtxtFile("E:/qtCode/heart-language-laboratory/TestMatlabLib/TestMatlabLib/nibi.txt");

    int len=data.size();
    // len=2000;
    qDebug()<<"len="<<len;
    ibi.set_size(len/2, 2);
    int t=0;
    for (int idx0{0}; idx0 < len/2; idx0++) {
        for (int idx1{0}; idx1 < 2; idx1++) {
            ibi[idx0 + len/2 * idx1] = data[t++];
        }
    }
    timeDomainHRV(ibi, 60, 50, &output);
    // qDebug()<<ibi.at(0,0)<<ibi.at(0,1);
    qDebug()<<output.max<<output.min<<output.mean<<output.HRVTi<<output.NNx<<output.TINN;
    qDebug()<<output.SDNN<<output.RMSSD<<output.sdHR<<output.pNNx<<output.SDNNi<<output.meanHR;
    struct0_T output_freq;
    const double VLF_tmp[2]={0,0.04};
    const double LF_tmp[2]={0.04,0.15};
    const double HF_tmp[2]={0.15,0.4};
    freqDomainHRV(ibi, VLF_tmp, LF_tmp, HF_tmp, 16, 256,128, 512, 2, &output_freq);
    // qDebug()<<ibi.at(0,0)<<ibi.at(0,1);
    qDebug()<<"ar_lfhf="<<output_freq.ar.hrv.LFHF<<output_freq.ar.hrv.peakLF;
    qDebug()<<"lomb_lfhf="<<output_freq.lomb.hrv.LFHF;
    qDebug()<<"welch_lfhf="<<output_freq.welch.hrv.LFHF;

    struct5_T output_non;
    nonlinearHRV(ibi, 3, 0.01, 4, 100, 13, &output_non);
    qDebug()<<"t1"<<ibi.at(0,0)<<ibi.at(0,1);
    // qDebug()<<output_non.dfa.alpha[0]<<output_non.dfa.alpha[1];

    struct7_T output_poin;
    poincareHRV(ibi, &output_poin);
    qDebug()<<"SD1="<<output_poin.SD1<<"SD2="<<output_poin.SD2;
    struct9_T output_tf;
    // Call the entry-point 'timeFreqHRV'.
    timeFreqHRV(ibi, VLF_tmp, LF_tmp, HF_tmp, 16, 30,15, 512, 2, &output_tf);
    qDebug()<<"ar.global.hrv.aVLF="<<output_tf.ar.global.hrv.aVLF<<"lomb.global.hrv.aLF="<<output_tf.ar.global.hrv.aLF;


}
QList<double> Widget::readtxtFile(const QString &filePath)
{
    QFile file(filePath);
    QList<double>data;
    coder::array<double, 2U> tmp;
    if (file.open(QIODevice::ReadOnly  | QIODevice::Text)) {
        qDebug() << "打开文件:" << filePath;
        QTextStream in(&file);
        int lineNumber = 0;
        while (!in.atEnd())  {
            lineNumber++;
            QString line = in.readLine().trimmed();
            // 跳过空行
            if (line.isEmpty())  {
                continue;
            }

            // 分割行数据
            QStringList columns = line.split(',',  Qt::SkipEmptyParts);

            // 确保有两列数据
            if (columns.size()  < 2) {
                qDebug() << "第" << lineNumber << "行数据格式错误，需要两列数据";
                continue;
            }

            // 获取两列数据并去除空格
            QString col1 = columns[0].trimmed();
            QString col2 = columns[1].trimmed();
            data.append(col1.toDouble());
            data.append(col2.toDouble());
        }
        file.close();
    }

    return data;
}

void Widget::on_pushButton_2_clicked()
{
    // coder::array<double, 2U> dibi;
    // coder::array<double, 2U> nibi;
    // coder::array<double, 2U> trend;
    // double resampleRate_tmp=4;
    // // Initialize function 'detrendIBI_priors' input arguments.
    // // Initialize function input argument 'ibi'.
    // coder::array<double, 2U> ibi;
    // QList<double> data=readtxtFile("E:/qtCode/heart-language-laboratory/TestMatlabLib/TestMatlabLib/nibi.txt");
    // int len=data.size();
    // qDebug()<<"len="<<len;
    // ibi.set_size(len/2, 2);
    // int t=0;
    // for (int idx0{0}; idx0 < len/2; idx0++) {
    //     for (int idx1{0}; idx1 < 2; idx1++) {
    //         ibi[idx0 + len/2 * idx1] = data[t++];
    //     }
    // }

    // // Call the entry-point 'detrendIBI_priors'.
    // detrendIBI_priors(ibi, resampleRate_tmp, 10,false, dibi, nibi, trend);
    // for(int i=0;i<10;i++)
    // {
    //     qDebug()<<"dibi"<<dibi.at(i,1);
    //     qDebug()<<"nibi"<<nibi.at(i,1);
    //     qDebug()<<"trend"<<trend.at(i,1);
    // }
}

